<template>
  
  <div>
  </div>
</template>

<script>
import { getRole } from "@/api/system/role";
import tab from './tab.vue'
import { chulikesu, getjyue, getjyues, zhenggetjyues } from "@/api/new";
import { treeselect as menuTreeselect } from "@/api/system/menu";

export default {
  name: "Role",
  dicts: ["sys_normal_disable"],

  components: { tab },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      zhengroleList: [],
      wanjiekopng: false,
      // 弹出层标题
      title: "",
      kesuwanfrom: {
        orderId: "",
        disposeResult: "",
      },
      kesuwanfromre: {
        disposeResult: [
          { required: true, message: "处理结果不能为空", trigger: "blur" },
        ],
      },
      datess: {
        startTime:'',endTime:''
      },
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项
      secvalue: "",
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      options: [
        {
          value: "按月查询",
          label: "按月查询",
        },
        {
          value: "按年查询",
          label: "按年查询",
        },
      ],
      // 查询参数
      queryParams: {
        startTime: '',
        endTime: '',
        pageNo: 1,
        pageSize: 10,
      },
      dates: {
        month: "",
        years: "",
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" },
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dangyue() {
      // 获取当前日期
      const now = new Date();

      // 获取年份，getFullYear()方法会返回一个四位的年份
      const year = now.getFullYear();

      // 获取月份，getMonth()方法返回的月份是从0开始的，所以需要加1
      const month = (now.getMonth() + 1).toString().padStart(2, "0");

      // 使用模板字符串或者字符串连接来组合年份和月份
      // return `${year}-${month}`;
      this.dates.month = `${year}-${month}`;
      this.getList();
    },
    dangnian() {
      const now = new Date();

      // 获取年份，getFullYear()方法会返回一个四位的年份
      const year = now.getFullYear();

      console.log(year);
      this.dates.years = `${year}`;
      this.getLists();
    },
    guanbishoukan() {
      this.kesuwanfrom = {
        orderId: "",
        disposeResult: "",
      };
    },
    wanjjie() {
      this.$refs["wanjie"].validate((valid) => {
        if (valid) {
          chulikesu(this.kesuwanfrom).then(() => {
            this.wanjiekopng = false;
            this.getList();
            this.$modal.msgSuccess("处理成功");
          });
        }
      });
    },

    /** 查询角色列表 */
    getList() {
      this.loading = true;
      getjyue({ date: this.dates.month }).then((response) => {
        this.roleList = response.data;
        this.loading = false;
      });
    },
    getLists() {
      this.loading = true;
      getjyues({ date: this.dates.years }).then((response) => {
        this.roleList = response.data;
        this.loading = false;
      });
    },

    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.dept.getCheckedKeys();
      // 半选中的部门节点
      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    zhenghandleQuery() {
      this.zhenggetList();
    },
    zhenggetList() {
      this.loading = true;
      zhenggetjyues(this.datess).then((response) => {
        this.zhengroleList = [response.data];
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    handleQuerys() {
      this.queryParams.pageNo = 1;
      this.getLists();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dates = {
        month: "",
        years: "",
      };
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDataScope":
          this.handleDataScope(row);
          break;
        case "handleAuthUser":
          this.handleAuthUser(row);
          break;
        default:
          break;
      }
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      if (type == "menu") {
        let treeList = this.menuOptions;
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
        }
      } else if (type == "dept") {
        let treeList = this.deptOptions;
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
        }
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value, type) {
      if (type == "menu") {
        this.$refs.menu.setCheckhandleAddedNodes(value ? this.menuOptions : []);
      } else if (type == "dept") {
        this.$refs.dept.setCheckedNodes(value ? this.deptOptions : []);
      }
    },
    // 树权限（父子联动）
    handleCheckedTreeConnect(value, type) {
      if (type == "menu") {
        this.form.menuCheckStrictly = value ? true : false;
      } else if (type == "dept") {
        this.form.deptCheckStrictly = value ? true : false;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getMenuTreeselect();
      this.open = true;
      this.title = "添加角色";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(this.ids);
      this.kesuwanfrom.orderId = row.orderId || this.ids[0];
      this.wanjiekopng = true;
      this.title = "处理";
    },
    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if (value !== "2") {
        this.$refs.dept.setCheckedKeys([]);
      }
    },
    /** 分配数据权限操作 */
    handleDataScope(row) {
      this.reset();
      const deptTreeSelect = this.getDeptTree(row.complaintId);
      getRole(row.complaintId).then((response) => {
        this.form = response.data;
        this.openDataScope = true;
        this.$nextTick(() => {
          deptTreeSelect.then((res) => {
            this.$refs.dept.setCheckedKeys(res.checkedKeys);
          });
        });
        this.title = "分配数据权限";
      });
    },
    /** 分配用户操作 */
    handleAuthUser: function (row) {
      const complaintId = row.complaintId;
      this.$router.push("/system/role-auth/user/" + complaintId);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/role/export",
        {
          ...this.queryParams,
        },
        `role_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
